<template>
  <view class="page">
    <button
      @click="autoAuditPic"
      :loading="onAuditing"
      :disabled="onAuditing"
      type="primary"
    >
      {{ onAuditing ? "等待中" : "自动审核" }}
    </button>
    <!-- <button @click="getAllPicId">获取id</button> -->
    <view v-show="picsWarning.length !== 0">
      <table class="pure-table pure-table-bordered">
        <tr>
          <th v-for="item in dict" :key="item">{{ item }}</th>
        </tr>
        <tr v-for="(item, ind1) in picsWarning" :key="(item, ind1)">
          <td>{{ ind1 + 1 }}</td>
          <td>{{ item.pic_id }}</td>
          <td>{{ item.pic_owner }}</td>
          <td>{{ item.inValidType }}</td>
          <td>{{ item.label }}</td>
          <td>
            <image
              class="thumbnail"
              :src="item.thumbnail"
              mode="aspectFill"
              @click="previewImg(item.thumbnail)"
              title="点击放大"
            ></image>
          </td>
          <td>
            <view class="option-btns">
              <view
                class="delete-option"
                @click="deletePic(ind1)"
                title="删除"
              ></view>
              <view
                class="accept-option"
                @click="whiteList(ind1)"
                title="设置白名单"
              ></view>
            </view>
          </td>
        </tr>
      </table>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      picsWarning: [
        // {
        //   pic_id: "xxx",
        //   pic_owner: "yy",
        //   inValidType: "zzz",
        //   label: "www",
        // },
      ],
      dict: ["序号", "图片id", "上传者", "违禁类别", "备注", "缩略图", "操作"],
      onAuditing: false,
      temp_pic_display: null,
      ivDict: {
        politics: "涉政",
        porn: "涉黄",
        terrorism: "恐怖",
      },
    };
  },
  methods: {
    autoAuditPic: function () {
      this.onAuditing = true;
      console.log("图片审核");
      setTimeout(() => {
        this.onAuditing = false;
      }, 1000);
      this.getAllPicId();
    },
    previewImg: function (e) {
      // console.log(e);
      if (e === null || e === undefined) {
        return;
      }
      uni.previewImage({
        urls: [e],
      });
    },
    getAllPicId: function () {
      let postData = {
        option: "getAllPictureId",
      };
      this.$http
        .post(this.$api.admin, postData)
        .then((res) => {
          let ids = res.data.getAllPictureId;
          // console.log(ids);
          return ids;
        })
        .then((ids) => {
          console.log(ids);
          for (let i = 0; i < ids.length; i++) {
            this.auditOneByOne(ids[i]);
          }
        })
        .catch((err) => {
          console.log(err);
          alert("图片id获取失败");
        });
    },
    auditOneByOne: function (pic_id) {
      let postData = {
        option: "autoReviewPicture",
        pic_id: pic_id,
      };
      this.$http
        .post(this.$api.admin, postData)
        .then((res) => {
          console.log(res.data);
          let result = res.data.result;
          return result;
        })
        .then((result) => {
          this.addInValidPic(pic_id, result);
        })
        .catch((err) => {
          console.log(err);
          alert(pic_id + "检测失败");
        });
    },
    addInValidPic: function (pic_id, result) {
      let inValidTypes = [];
      let label = "";
      if (result.suggestion !== "pass") {
        let cs = result.categorySuggestions;
        for (let it in cs) {
          if (cs[it] !== "pass") {
            // console.log("cs", cs[it]);
            // console.log("it", this.ivDict[it]);
            inValidTypes.push(this.ivDict[it]);
            if (it === "politics") {
              label = result.detail.politics[0].label;
            }
          }
        }
        let inValidType = inValidTypes.join();
        console.log(inValidType);

        this.getInValidPic(pic_id);
        this.$http
          .post(this.$api.admin, {
            option: "getPictureInfo",
            pic_id: pic_id,
          })
          .then((res) => {
            let picture = res.data.Picture;
            // console.log(res.data);
            let picobj = {
              pic_id:
                "**" + picture.pic_id.substring(picture.pic_id.length - 7),
              pic_owner:
                "**" +
                picture.pic_album.substring(picture.pic_album.length - 7),
              label: label,
              inValidType: inValidType,
              thumbnail: this.temp_pic_display,
            };
            console.log(picobj);
            this.picsWarning.push(picobj);
          })
          .catch((err) => {
            console.log(err);
            alert("图片信息获取失败");
          });
      }
    },
    getInValidPic: function (pic_id) {
      let postData = {
        option: "download",
        pic_id: pic_id,
      };
      this.$http
        .download(this.$api.admin, { params: postData })
        .then((res) => {
          console.log(res);
          this.temp_pic_display = res.tempFilePath;
        })
        .catch((err) => {
          console.log(err);
          alert("图片获取失败");
        });
    },
    whiteList: function (index) {
      this.picsWarning.splice(index, 1);
    },
    deletePic: function (index) {
      let picobj = this.picsWarning[index];
      console.log(picobj);
      uni.showModal({
        title: "提示",
        content: "是否删除该图片",
        confirmText: "删除",
        confirmColor: "red",
        cancelText: "取消",
        success: (res) => {
          let postData = {
            option: "deletePicture",
            pic_id: picobj.pic_id,
          };
          this.$http
            .post(this.$api.admin, postData)
            .then((res) => {
              let status = res.data.status;
              switch (status) {
                case "100": {
                  uni.showToast({ title: "删除成功" });
                  this.picsWarning.splice(index, 1);
                  break;
                }
                case "101": {
                  alert("删除失败");
                  break;
                }
              }
            })
            .catch((err) => {
              console.log(err);
              alert("删除请求出错");
            });
        },
        fail: (err) => {
          console.log(err);
          alert("删除操作失败");
        },
      });
    },
  },
};
</script>

<style>
.option-btns {
  display: flex;
  justify-content: space-around;
}
.page {
  max-width: 1200px;
  margin: 75px auto;
  background-color: white;
  height: 100%;
  font-size: 20px;
  padding: 40px;
  box-shadow: 0 0 20px rgb(48, 98, 48);
}
.inline-text {
  font-size: 20px;
}
.thumbnail {
  width: 60px;
  height: 60px;
  cursor: pointer;
}
</style>